package ejercicio.practica6;

/*tareaRed.java
 @author A.Tomeu
 */
import java.io.*;
import java.net.*;

public class tareaRed implements Runnable {

    private String dir;
    private String datos;
    private RandomAccessFile volcado;
    private URL url;

    public tareaRed(String d) {
        dir = d;
    }

    @Override
    public void run() {
        try {
            url = new URL(dir);
        } catch (MalformedURLException e) {
        }
        System.out.println("Contactando con " + dir);
        try {
            Thread h = Thread.currentThread();
            volcado = new RandomAccessFile(h.getName() + ".html", "rw");
            BufferedReader lector = new BufferedReader(new InputStreamReader(url.openStream()));
            do {
                datos = lector.readLine();
                if (datos != null) {
                    volcado.writeChars(datos);
                }
                //System.out.println("escribiendo...");   
            } while (datos != null);
            volcado.close();
        } catch (IOException e) {
        }
    }
}
